package com.woniuxy.produce.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.woniuxy.common.dto.SkuDto;
import com.woniuxy.produce.es.SkuEs;
import com.woniuxy.produce.pojo.FinanceSku;
import com.woniuxy.produce.service.FinanceSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    private ReactiveStringRedisTemplate redisTemplate;

//    @Autowired
//    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private FinanceSkuService financeSkuService;

    @Autowired
    private FinanceSkuService skuService;

    @RequestMapping("/query/{id}")
    public FinanceSku queryByid(@PathVariable("id") Integer id){
        FinanceSku financeSku = financeSkuService.getById(id);
        return financeSku;
    }

    //查询sku列表
    @GetMapping("/skuList")
    public List<FinanceSku> skuList(){
        return financeSkuService.list();
    }

    @GetMapping("/updateStock")
    public void updateStock(){
        List<SkuDto> skuDtos = financeSkuService.queryAllCartSku();
        for (SkuDto skuDto : skuDtos) {
            FinanceSku financeSku = financeSkuService.getById(skuDto.getId());
            financeSku.setFinanceSkuStock(financeSku.getFinanceSkuStock()-skuDto.getCount());
            financeSkuService.updateById(financeSku);
        }
    }

    /**
     * 减库存
     * @param skuId
     * @return
     */
    @PostMapping("/updateSkuByskuId")
    public Boolean updateSku(@RequestBody Long skuId){
        return skuService.lambdaUpdate()
                .eq(FinanceSku::getId,skuId)
                .setSql("finance_sku_stock=finance_sku_stock-1")
                .update();
    }

    @PostMapping("/updateSku")
    public Boolean updateSku(@RequestBody FinanceSku sku){
        return skuService.updateById(sku);
    }

    @Autowired
    private SkuEs skuEs;

    //初始化sku
    @PostMapping("/initSku")
    public void initSku(){
        skuEs.saveAll(skuService.list());
    }

    //查询sku列表
    @GetMapping("/getSkuList")
    public Iterable<FinanceSku> getSkuList(){
        return skuEs.findAll();
    }

    /**
     * 根据描述分页查询sku
     * @param describe
     * @return
     */
    @PostMapping("/getSkuByDescribe")
    public Page<FinanceSku> getSkuByDescribe(String describe,int page,int size){
        Pageable pageable = PageRequest.of(page,size);
        return skuEs.findFinanceSkuByFinanceSkuDescribe(describe,pageable);
    }
}
